<?php
/**
 * Created by 784855684@qq.com.
 * Link: https://github.com/lichtung/wshore
 * User: linzh
 * Date: 7/8/17
 * Time: 11:28 AM
 */
declare(strict_types=1);


namespace application\module\admin\addon\wechat\model;

use application\module\admin\addon\model\AddonModel;
use application\module\admin\addon\Wechat;
use application\module\admin\addon\wechat\entity\Member;
use application\module\admin\addon\wechat\throws\UnsubscribeException;
use wshore\throws\DatabaseException;
use wshore\throws\db\RecordNotFoundException;

/**
 * Class MemberModel
 *
 *
 * @property int $id
 * @property string $openid
 * @property string $nickname
 * @property int $sex
 * @property string $language
 * @property string $city
 * @property string $province
 * @property string $country
 * @property string $headimgurl
 * @property int $subscribe
 * @property int $subscribe_time 关注的时间
 * @property int $unsubscribe_time 取消关注的时间
 * @property string $remark
 * @property int $groupid
 * @property int $subscribetimes
 *
 * @property int $fromweb 1-客户信息来源于web,2-客户信息来自于关注
 *
 * @package application\module\admin\addon\wechat\model
 */
class MemberModel extends AddonModel
{

    protected function tablePrefix()
    {
        return 'ws_';
    }

    protected function tableName()
    {
        return 'wechat_member';
    }

    /**
     * MemberModel constructor.
     * @param null $id
     * @throws RecordNotFoundException
     * @throws DatabaseException 自动保存数据出错
     */
    public function __construct($id = null)
    {
        if (is_numeric($id)) {
            parent::__construct($id);
        } elseif ($id and is_string($id) and strlen($id) > 20) {
            # openid
            parent::__construct();
            $data = $this->where(['openid' => $id])->find();
            if (!$data) {
                $sdk = Wechat::getInstance();
                try {
                    $data = $sdk->getSubscribeUserInfo($id);
                    $this->saveFromMemberEntity(new Member($data));
                } catch (UnsubscribeException $exception) {
                    throw new RecordNotFoundException($id);
                }
            } else {
                $this->id = $data['id'];
                $this->data = $data;
            }
        } elseif ($id and $id instanceof Member) {
            parent::__construct();
            $member = &$id;
            $data = $this->where(['openid' => $member->openid])->find();
            if ($data) {
                $this->id = $data['id'];
                $this->data = $data;
            } else {
                $this->saveFromMemberEntity($member);
            }
        } else {
            parent::__construct();
            # to create
        }
    }

    private function saveFromMemberEntity(Member $member)
    {
        $this->openid = $member->openid;
        $this->nickname = $member->nickname;
        $this->sex = $member->sex;
        $this->language = $member->language;
        $this->city = $member->city;
        $this->province = $member->province;
        $this->country = $member->country;
        $this->headimgurl = $member->headimgurl;
        return $this->save(false);
    }

    protected function struct(): string
    {
        return "  `id` int(10) unsigned zerofill NOT NULL AUTO_INCREMENT,
  `subscribe` tinyint(4) unsigned NOT NULL DEFAULT '1' COMMENT '是否关注',
  `openid` varchar(40) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `nickname` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `sex` tinyint(4) NOT NULL,
  `language` varchar(10) NOT NULL,
  `city` varchar(64) NOT NULL,
  `province` varchar(64) NOT NULL,
  `country` varchar(64) NOT NULL,
  `headimgurl` varchar(255) DEFAULT NULL,
  `subscribe_time` int(11) DEFAULT '0',
  `remark` varchar(255) DEFAULT '',
  `groupid` int(11) DEFAULT '0',
  `subscribetimes` int(255) unsigned NOT NULL DEFAULT '0' COMMENT '关注次数',
  `unsubscribe_time` int(11) NOT NULL DEFAULT '0',
  `fromweb` tinyint(3) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`,`openid`)";
    }


}